package com.inhim.pjhim.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.provider.MediaStore;

import com.inhim.pjhim.app.MyApplication;
import com.inhim.pjhim.entity.ProjectListEntity;
import com.inhim.pjhim.myinterface.IMusic;
import com.inhim.pjhim.utils.PrefUtils;

import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCMediaManager;
import fm.jiecao.jcvideoplayer_lib.JCRadioPlayerStandard;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class PlayerService extends Service {
    private MediaPlayer mp;
    private MyApplication application;
    private List<String> songs;
    private int songItemPos;
    private JCRadioPlayerStandard mJcVideoPlayerStandard;
    private List<ProjectListEntity.List> projectListEntitys;

    @Override
    public void onCreate() {
        super.onCreate();
        application = (MyApplication) getApplication();
        mJcVideoPlayerStandard=application.getJCRadioPlayerStandard();
        projectListEntitys=application.getProjectListEntitys();
        songItemPos=application.getSongItemPos();
    }

    @Override
    public IBinder onBind(Intent intent) {
        play(songItemPos);
        return new MusicListener();
    }

    public void play(int position) {
        //<span style="white-space:pre">		</span>//传入播放位置在列表中的边界
        List<ProjectListEntity.List> mProjectListEntitys=application.getProjectListEntitys();
        if(position>=0){
            position=position % mProjectListEntitys.size();
        }
        else{
            position = mProjectListEntitys.size()+ position;
        }

        application.setSongItemPos(position);//在全局变量中标记当前播放位置
       playRadio(position);
    }

    private void playRadio(int position) {
        mJcVideoPlayerStandard.setUp(projectListEntitys.get(position).getUrl()
                , JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, position);
        try {
            //if(JCMediaManager.instance().mediaPlayer.isPlaying() && JCMediaManager.instance().CURRENT_PLAYING_URL.equals(videoUrl)){
                //mJcVideoPlayerStandard.setIconStatus();
                mJcVideoPlayerStandard.playRadio();
                //PrefUtils.putBoolean(isPlayingKey,false);
                //PrefUtils.putString(isPlayingUrl,"");
            //}
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        mp.stop();
        mp.release();
        mp = null;
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    //<span style="white-space:pre">	</span>//实现IMusic接口，用于接收播放信息
    private class MusicListener extends Binder implements IMusic {

        @Override
        public void moveon() {
            mJcVideoPlayerStandard.playRadio();
        }

        @Override
        public void pause() {
            mJcVideoPlayerStandard.pauseRadio();
        }

        @Override
        public void stop() {
            /*if (mp != null) {
                mJcVideoPlayerStandard.();
                mp.release();
                mp = null;
            }*/
        }

        @Override
        public void nextSong() {
            int playPosition=application.getSongItemPos();
            play(++playPosition);
        }

        @Override
        public void lastSong() {
            int playPosition=application.getSongItemPos();
            play(--playPosition);
        }
    }

}
